package com.rj.framework.component.interceptor.impl;

import com.rj.framework.component.BusinessComponent;
import com.rj.framework.component.interceptor.ComponentInterceptor;
import com.rj.framework.context.Context;
import lombok.extern.slf4j.Slf4j;

/**
 * 性能监控
 *
 * @author YFX_RANLUBO
 * @date 2025/06/27 13:57
 **/
@Slf4j
public class MonitoringInterceptor implements ComponentInterceptor {
    @Override
    public <I, O, C extends Context<?>> BusinessComponent<I, O, C> intercept(BusinessComponent<I, O, C> component, String componentName) {
        return (input, context) -> {
            long startTime = System.currentTimeMillis();
            try {
                O result = component.execute(input, context);
                return result;
            } catch (Exception e) {
                throw e;
            } finally {
                log.info("组件{} 耗时: {}ms", componentName, (System.currentTimeMillis() - startTime));
            }
        };
    }
}
